package top.mcwebsite.playandroid.business.home.impl.ui

import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.MailOutline
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.ScrollableTabRow
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintSet
import androidx.constraintlayout.compose.Dimension
import androidx.constraintlayout.compose.ExperimentalMotionApi
import androidx.constraintlayout.compose.MotionLayout
import top.mcwebsite.playandroid.business.home.impl.vm.MainIntent
import top.mcwebsite.playandroid.business.home.impl.vm.MainViewModel
import top.mcwebsite.playandroid.business.home.impl.mainTabItems
import top.mcwebsite.playandroid.strings.Str

@OptIn(ExperimentalMotionApi::class)
@Composable
fun CollapsingHeader(
    collapsingState: CollapsingState,
    viewModel: MainViewModel,
) {
    val uiState by viewModel.uiStateFlow.collectAsState()
    val value by animateFloatAsState(
        targetValue = if (collapsingState == CollapsingState.EXPANDED) 0f else 1f,
        animationSpec = tween(
            durationMillis = 300,
            easing = FastOutSlowInEasing,
        ), label = ""
    )
    MotionLayout(
        start = ConstraintSet {
            val firstRow = createRefFor("firstRow")
            val secondRow = createRefFor("secondRow")
            constrain(firstRow) {
                top.linkTo(parent.top)
                bottom.linkTo(secondRow.top)
                width = Dimension.matchParent
            }
            constrain(secondRow) {
                top.linkTo(firstRow.bottom)
            }
        },
        end = ConstraintSet {
            val firstRow = createRefFor("firstRow")
            val secondRow = createRefFor("secondRow")
            constrain(firstRow) {
                bottom.linkTo(parent.top)
                width = Dimension.matchParent
            }
            constrain(secondRow) {
                top.linkTo(firstRow.bottom)
            }

        },
        progress = value,
    ) {
        Row(
            modifier = Modifier
                .padding(vertical = 2.dp, horizontal = 12.dp)
                .layoutId("firstRow")
        ) {
            Search(
                modifier = Modifier
                    .height(28.dp)
                    .weight(1f)
                    .align(Alignment.CenterVertically)
                    .clickable {
                        viewModel.sendIntent(MainIntent.GoSearchIntent)
                    }
            )
            IconButton(
                modifier = Modifier.align(Alignment.CenterVertically),
                onClick = {
                    viewModel.sendIntent(MainIntent.GoMessageIntent)
                }
            ) {
                Icon(
                    imageVector = Icons.Outlined.MailOutline,
                    contentDescription = stringResource(Str.notification),
                    modifier = Modifier
                        .size(22.dp)
                )
            }
        }
        ScrollableTabRow(
            edgePadding = 0.dp,
            selectedTabIndex = mainTabItems.indexOf(uiState.currentTab),
            modifier = Modifier.layoutId("secondRow")
        ) {
            mainTabItems.forEach { item ->
                Box(
                    Modifier
                        .clickable {
                            viewModel.sendIntent(MainIntent.OnTabClickIntent(item))
                        },
                    contentAlignment = Alignment.Center,
                ) {
                    Text(text = stringResource(id = item.strRes), modifier = Modifier.padding(bottom = 5.dp))
                }
            }
        }
    }

}

